package com.micro.school.edu.service.impl;

import java.util.List;

import com.alibaba.druid.util.StringUtils;
import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.DateUtils;
import com.micro.school.common.core.utils.SecurityContextHolderUtils;
import com.micro.school.common.security.utils.SecurityUtils;
import com.micro.school.edu.convert.CollegeModelConvert;
import com.micro.school.edu.domain.CollegeModel;
import com.micro.school.edu.domain.vo.CollegeMajorVo;
import com.micro.school.edu.domain.vo.CollegeModelVo;
import com.micro.school.edu.mapper.CollegeModelMapper;
import com.micro.school.edu.service.ICollegeModelService;
import com.micro.school.system.api.RemoteOrderService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.vo.CollegeModelVos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import com.micro.school.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


/**
 * 学院模式Service业务层处理
 *
 * @author rakic
 * @date 2025-08-07
 */
@Service
public class CollegeModelServiceImpl implements ICollegeModelService
{
    @Autowired
    private CollegeModelMapper collegeModelMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteOrderService remoteOrderService;

    /**
     * 查询学院模式
     *
     * @param id 学院模式主键
     * @return 学院模式
     */
    @Override
    public CollegeModelVo selectCollegeModelById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return collegeModelMapper.selectCollegeModelById(id);
    }

    /**
     * 查询学院模式
     *
     * @param id 学院模式主键
     * @return 学院模式
     */
    @Override
    public CollegeModelVos selectCollegeModelByIds(Long id)
    {
        return collegeModelMapper.selectCollegeModelByIds(id,SecurityContextHolder.getUserTenantId());
    }

    /**
     * 查询学院模式列表
     *
     * @param collegeModelVo 学院模式
     * @return 学院模式
     */
    @Override
    public List<CollegeModelVo> selectCollegeModelList(CollegeModelVo collegeModelVo)
    {
        collegeModelVo.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            collegeModelVo.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
            collegeModelVo.setCollegeIds(StringUtils.isEmpty(SecurityContextHolder.getUserCollegeId()) ? null :SecurityContextHolder.getUserCollegeId());
        }
        return collegeModelMapper.selectCollegeModelList(collegeModelVo);
    }

    /**
     * 新增学院模式
     *
     * @param collegeModelVo 学院模式
     * @return 结果
     */
    @Override
    public int insertCollegeModel(CollegeModelVo collegeModelVo)
    {
        CollegeModel collegeModel = CollegeModelConvert.INSTANCE.convert(collegeModelVo);

        collegeModel.setCreateTime(DateUtils.getNowDate());
        collegeModel.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        StudentInfoVos studentInfoVos = remoteUserService.selectSchoolById(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        collegeModel.setSchoolName(studentInfoVos.getSchoolName());
        return collegeModelMapper.insertCollegeModel(collegeModel);

    }

    /**
     * 修改学院模式
     *
     * @param collegeModelVo 学院模式
     * @return 结果
     */
    @Override
    public int updateCollegeModel(CollegeModelVo collegeModelVo)
    {
        CollegeModel collegeModel = CollegeModelConvert.INSTANCE.convert(collegeModelVo);
        collegeModel.setUpdateTime(DateUtils.getNowDate());
        CollegeModelVo collegeModelVo1 = collegeModelMapper.selectCollegeModelById(collegeModelVo.getId());
        if (!collegeModelVo.getModelName().equals(collegeModelVo1.getModelName())){
            remoteUserService.updateAllCollegeModel(collegeModelVo.getModelName(),collegeModelVo1.getId(),collegeModelVo1.getModelName());
            remoteOrderService.updateAllCollegeModel(collegeModelVo.getModelName(),collegeModelVo.getId());
        }
        return collegeModelMapper.updateCollegeModel(collegeModel);
    }

    /**
     * 批量删除学院模式
     *
     * @param ids 需要删除的学院模式主键
     * @return 结果
     */
    @Override
    public int deleteCollegeModelByIds(List<Long> ids)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return collegeModelMapper.deleteBatchIds(ids);
    }

    /**
     * 删除学院模式信息
     *
     * @param id 学院模式主键
     * @return 结果
     */
    @Override
    public int deleteCollegeModelById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return collegeModelMapper.deleteById(id);
    }

    @Override
    public List<CollegeModelVos> selectCollegeModelLists(CollegeModelVos collegeModelVos) {
        collegeModelVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            collegeModelVos.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
            collegeModelVos.setCollegeIds(StringUtils.isEmpty(SecurityContextHolder.getUserCollegeId()) ? null: SecurityContextHolder.getUserCollegeId());
        }
        return collegeModelMapper.selectCollegeModelLists(collegeModelVos);
    }

    @Override
    public List<CollegeModelVos> selectCollegeModelLists1(CollegeModelVos collegeModelVos) {
        collegeModelVos.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        return collegeModelMapper.selectCollegeModelLists(collegeModelVos);
    }

}
